<template>
	<view class="page flex-col">
		<uni-nav-bar color="white" @clickLeft="back" :fixed="true" :background-color="navColor" status-bar
			left-icon="left" :border="false">
			<view class="top">
				选择商品
			</view>
		</uni-nav-bar>
		<view class="head">
			<view class="action">
				<view class="input-view">
					<input class="nav-bar-input" v-model="searchstr" type="text" placeholder="搜索商品" />

				</view>
				<image src="../../static/icon/search.png" @tap="search" mode="aspectFit"></image>
			</view>
		</view>
		<scroll-view :scroll-y="true" class="body" @scrolltolower="list()">
			<view class="group_23 flex-col">
				<block v-for="item in products">
					<view class="block_5 flex-row" @tap="select(item)">
						<image class="section_1 flex-col" mode="aspectFill" :src="image(item.image)"></image>
						<view class="text-wrapper_27 flex-col">
							<text class="text_4">{{item.name}}</text>
						</view>

					</view>
					<view class="divide"></view>
				</block>
			</view>

		</scroll-view>
	</view>
</template>
<script>
	import helper from "../../utils/helper";
	import {
		agingApi as agingApi
	} from "../../utils/api";
	export default {
		data() {
			return {
				searchstr:"",
				navColor: 'transparent',
				products: [],
				productsv: [],
				listall:[],
				pageindex:1,
				more:true,
				currentIndex:null
			};
		},
		
		onLoad(e) {
			this.currentIndex=parseInt(e.index) 
		},
		onShow() {
			
				this.products=[],
				this.productsv=[],
				this.more=true		
			this.list()
		},
		methods: {
			search() {
				this.products=[],
				this.more=true
				this.pageindex=1
				this.list()
			},
			image(md5) {
				return helper.getImageUrl(md5);
			},
			async list() {
				if(!this.more)return
				var result = await agingApi.getproducts({
					key:this.searchstr,
					index: this.pageindex,
					pageSize: 10,
				});

				if (result.data) {
					this.products = this.products.concat(result.data.items)
					
					if(result.data.totalCount>this.products.length){
										   this.pageindex+=1
					}else{
										   this.more=false
					}
				}
			},
			
			select(item){
				
				uni.$emit("addpro",item)
				uni.navigateBack({
					delta: 1
				})
			},
			back() {
				uni.$off('addpro')
				uni.navigateBack({
					delta: 1
				})
			},
		},
	};
</script>
<style lang="scss">
	@import '../common/common.scss';
	@import './productlist.scss';
</style>